//
//  RootViewController.m
//  BLDMusic
//
//  Created by lxd on 16/3/17.
//  Copyright © 2016年 白云杰. All rights reserved.
//

#import "RootViewController.h"
#import "SectionModel.h"

@interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, PlayViewBarDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *sourceList;


@end

@implementation RootViewController {
    GooeySlideMenu *menu;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  
    self.navigationController.navigationBar.translucent = NO;
    [PlayViewController sharePlayViewBar].hidden = NO;
//    NSLog(@"DEMOFirstViewController will appear");
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    NSLog(@"DEMOFirstViewController will disappear");

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCollection];
//    [self automaticallyAdjustsScrollViewInsets];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    PlayViewController *bar = [PlayViewController sharePlayViewBar];
    bar.delegate = self;
    
    [window addSubview:bar];

    [self createBarButton];

}

#pragma mark - 创建collection
- (void)createCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGTH - 49 - 64);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - 113) collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.bounces = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    // 注册cell
    // 我的
    [_collectionView registerClass:[MineCollectionViewCell class] forCellWithReuseIdentifier:@"mineCell"];
    // 发现
    [_collectionView registerClass:[FindCollectionViewCell class] forCellWithReuseIdentifier:@"findCell"];
    // 推荐
    [_collectionView registerClass:[RecommendCollectionViewCell class] forCellWithReuseIdentifier:@"recCell"];
   
}

- (void)createBarButton {
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(setItemAction:)];
    [self.navigationItem.leftBarButtonItem setImage:[UIImage imageNamed:@"iconfont-shezhi"]];
    
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-iconsousu.png"] style:UIBarButtonItemStyleDone target:self action:@selector(searchItemAction:)];


    self.navigationItem.rightBarButtonItem = searchItem;

}

- (void)setItemAction:(UIBarButtonItem *)item {
    [PlayViewController sharePlayViewBar].hidden = YES;
    
    [self.sideMenuViewController presentLeftMenuViewController];
}

- (void)searchItemAction:(UIBarButtonItem *)item {
    
    BYJSearchViewController *VC = [[BYJSearchViewController alloc] init];

    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - collection的协议方法
#pragma mark 返回每个分区个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}
#pragma mark 返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mineCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        self.navigationItem.title = @"我的";
        return cell;
    } else if (indexPath.row == 1) {
        FindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"findCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor blueColor];
        self.navigationItem.title = @"发现";
        return cell;
    } else {
        RecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recCell" forIndexPath:indexPath];
        self.navigationItem.title = @"推荐";
        return cell;
    }
}

- (void)popPlayingView
{
    
    DSYMusicViewController *musicVC = [DSYMusicViewController sharedMusicPlayingVC];
    
    NSMutableArray *songList = [DSYDataManager shareDataManager].allDataArray;

    if (songList.count <= 0) {
        return;
    }
    
    musicVC.index = -1;
    DSYNAVViewController *nav = [DSYNAVViewController shareNavWithRootViewController:musicVC];
    // 在即将弹出来的时候才隐藏
    [PlayViewController sharePlayViewBar].hidden = YES;
    [self presentViewController:nav animated:YES completion:nil];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
